package com.eling.elcms.travel.webapp.controller;


import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import com.eling.elcms.travel.model.Mail;
import com.eling.elcms.travel.model.TravelContract;
import com.eling.elcms.travel.model.view.MailRegisterView;
import com.eling.elcms.travel.model.view.PlanMailView;
import com.eling.elcms.travel.service.IMailManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Controller
/*@RequestMapping("/api/mail")  访问路径的改写*/
public class MailController extends BaseFormController{
	
	@Autowired
	IMailManager mailManager;
	
	@RequestMapping("/api/mail/search*")
	@ResponseBody
	public List<Mail> sreach(@ModelAttribute SearchCondition sc,@ModelAttribute Mail mail) {
		sc.setQueryCondition(mail);
		return mailManager.search(sc); 
	}
	
	
	@RequestMapping("/api/mail/searchmail*")
	@ResponseBody
	public List<MailRegisterView> sreachMail(@ModelAttribute SearchCondition sc,@ModelAttribute TravelContract travelContract
			,@ModelAttribute Mail cond) {
		sc.setQueryCondition(travelContract);
		return mailManager.searchMail(sc,cond); 
	}
	
			
	
	@RequestMapping("/api/mail/query")
    @ResponseBody
    public List<Mail> query(@ModelAttribute Mail cond){
    	return mailManager.query(cond);
    }
	
	
	@RequestMapping("/api/mail/save")
    @ResponseBody
    public Mail save(@ModelAttribute@ModelFilling Mail feedcontent) {
		return mailManager.save(feedcontent);
    }
	
	
	@RequestMapping("/api/mail/{pkMail}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkMail) {
		mailManager.remove(pkMail);
	    return new Msg("删除成功");
	}
	
	
	@RequestMapping("/api/mail/queryplan")
    @ResponseBody
    public List<PlanMailView> queryplan(@RequestParam(value = "year", required = false) String year){
    	return mailManager.queryPlan(year); 
    }
			
	
	@RequestMapping("/api/mail/queryall")
    @ResponseBody
    public List<MailRegisterView> queryAll(@ModelAttribute Mail cond,@ModelAttribute TravelContract travelContract,
    		@RequestParam(value = "flag") Boolean flag){
    	return mailManager.queryAll(cond, travelContract,flag); 
    }
				
	
	@RequestMapping("/api/mail/savemail")
    @ResponseBody
    public List<Mail> save(@ModelAttribute@ModelFilling Mail cond
    		,@RequestParam(value = "pkPlan", required = false) Long pkPlan
    		,@RequestParam(value = "address", required = false) Long addressId) {
		return mailManager.saveMailRegister(cond, pkPlan,addressId);
    }
	
	
	@RequestMapping("/api/mail/toexcel")
	@ResponseBody
	public void toExcel(HttpServletResponse resp,
			@RequestParam(value = "status", required = false) String status,
			@RequestParam(value = "plan") Long pkPlan,
			@RequestParam(value = "productPk") Long pkProduct){
		mailManager.toExcel(resp,status,pkPlan,pkProduct);
	}
}
